package cn.minhx.domain.strategy.repository;

import cn.minhx.domain.strategy.model.entity.StrategyAwardEntity;
import cn.minhx.domain.strategy.model.entity.StrategyEntity;
import cn.minhx.domain.strategy.model.entity.StrategyRuleEntity;
import cn.minhx.domain.strategy.model.valobj.StrategyAwardRuleModelVO;
import cn.minhx.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import cn.minhx.domain.strategy.model.valobj.tree.RuleTreeVO;

import java.util.List;
import java.util.Map;

/**
 * 策略服务仓储接口
 *
 * @author min
 */
public interface IStrategyRepository {

    /**
     * 查询策略奖品
     *
     * @param strategyId 策略ID
     * @return 奖品集合
     */
    List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId);

    /**
     * 根据策略ID+奖品ID的唯一值组合，查询奖品信息
     *
     * @param strategyId 策略ID
     * @param awardId    奖品ID
     * @return 奖品信息
     */
    StrategyAwardEntity queryStrategyAwardEntity(Long strategyId, Long awardId);

    /**
     * 存储策略概率范围
     * 存储策略对应的奖品查找表
     *
     * @param key                      策略ID(或者策略ID_积分数)
     * @param rateRange                概率范围
     * @param shuffledAwardSearchTable 奖品查找表，key为生成的随机数(抽中的数)，通过这个随机数查找奖品
     */
    void storeStrategyAwardSearchRateTable(String key, Integer rateRange, Map<Integer, Long> shuffledAwardSearchTable);

    /**
     * 获取概率范围
     *
     * @param strategyId 策略ID
     * @return 抽奖策略对应的概率范围
     */
    Integer getRateRange(Long strategyId);

    /**
     * 获取概率范围
     *
     * @param key key(策略ID或者策略ID_权重积分)
     * @return 抽奖策略对应的概率范围
     */
    Integer getRateRange(String key);

    /**
     * 通过概率值获取奖品
     *
     * @param key     策略ID(或者策略ID_积分数)
     * @param rateKey 概率值
     * @return 奖品ID
     */
    Long getStrategyAwardAssemble(String key, Integer rateKey);

    /**
     * 查询抽奖策略
     *
     * @param strategyId 策略ID
     * @return 策略规则
     */
    StrategyEntity queryStrategyEntityByStrategyId(Long strategyId);

    /**
     * 查询策略规则 - 权重
     *
     * @param strategyId 抽奖策略
     * @param ruleModel  规则标识字符
     * @return 策略规则
     */
    StrategyRuleEntity queryStrategyRule(Long strategyId, String ruleModel);

    /**
     * 查询策略规则值
     *
     * @param strategyId 策略ID
     * @param ruleModel  规则标识字符
     * @return 策略规则值
     */
    String queryStrategyRuleValue(Long strategyId, String ruleModel);

    /**
     * 查询策略规则值
     *
     * @param strategyId 策略ID
     * @param awardId    奖品ID - 非必填
     * @param ruleModel  规则标识字符
     * @return 策略规则值
     */
    String queryStrategyRuleValue(Long strategyId, Long awardId, String ruleModel);

    /**
     * 查询策略奖品规则值
     *
     * @param strategyId 策略ID
     * @param awardId    奖品ID
     * @return 奖品规则值
     */
    StrategyAwardRuleModelVO queryStrategyAwardRuleModelVO(Long strategyId, Long awardId);

    /**
     * 根据规则树ID，查询树结构信息
     *
     * @param treeId 规则树ID
     * @return 树结构信息
     */
    RuleTreeVO queryRuleTreeVOByTreeId(String treeId);

    /**
     * 缓存奖品库存
     *
     * @param cacheKey   key
     * @param awardCount 库存值
     */
    void cacheStrategyAwardCount(String cacheKey, Integer awardCount);

    /**
     * 奖品库存扣减
     *
     * @param cacheKey 奖品库存缓存key
     * @return 扣减结果
     */
    Boolean subtractAwardStock(String cacheKey);

    /**
     * 写入奖品库存消费队列
     *
     * @param strategyAwardStockKeyVO vo
     */
    void awardStockConsumeSendQueue(StrategyAwardStockKeyVO strategyAwardStockKeyVO);

    /**
     * 获取奖品库存消费队列
     */
    StrategyAwardStockKeyVO takeQueueValue();

    /**
     * 更新奖品库存消耗
     *
     * @param strategyId 策略ID
     * @param awardId 奖品ID
     */
    void updateStrategyAwardStock(Long strategyId, Long awardId);
}